package com.zeldu.video.controller;


import com.zeldu.common.model.response.CommonCode;
import com.zeldu.common.model.response.QueryResponseResult;
import com.zeldu.common.model.response.QueryResult;
import com.zeldu.common.model.response.ResponseResult;
import com.zeldu.video.pojo.Video;
import com.zeldu.video.service.VideoService;
import com.zeldu.video.utils.LiveSpider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("")
public class VideoController {

    @Autowired(required = false)
    private VideoService videoService;


    @RequestMapping("queryall")
    @ResponseBody
    public QueryResponseResult<Video> queryAll(){
        QueryResult<Video> queryResult=new QueryResult<>();
        List<Video> videoList=videoService.queryAll();
        this.setStringId(videoList);
        queryResult.setList(videoList);
        queryResult.setTotal(videoList.size());
        return new QueryResponseResult<>(CommonCode.SUCCESS,queryResult);
    }

    @PostMapping("queryrecorded")
    @ResponseBody
    public QueryResponseResult<Video> queryRecorded(@RequestParam("course_id") Long course_id,
                                                    @RequestParam("id") Long id){
        QueryResult<Video> queryResult=new QueryResult<>();
        List<Video> videoList=videoService.queryRecorded(course_id, id);
        this.setStringId(videoList);
        queryResult.setList(videoList);
        queryResult.setTotal(videoList.size());
        if(videoList.isEmpty()){
            return new QueryResponseResult<>(CommonCode.FAIL,queryResult);
        }else{
            return new QueryResponseResult<>(CommonCode.SUCCESS,queryResult);
        }

    }

    @PostMapping("querylive")
    @ResponseBody
    public QueryResponseResult<Video> queryLive(@RequestParam("course_id") Long course_id){
        QueryResult<Video> queryResult=new QueryResult<>();
        List<Video> videoList=videoService.queryLive(course_id);
//        LiveSpider liveSpider=new LiveSpider();
//        try {
//            videoList.get(0).setUrl(liveSpider.getDouyuLive(videoList.get(0).getUrl()));
//        }catch (Exception e){
//            System.out.println("直播链接解析出错");
//        }
        this.setStringId(videoList);
        queryResult.setList(videoList);
        queryResult.setTotal(videoList.size());
        if(videoList.isEmpty()){
            return new QueryResponseResult<>(CommonCode.FAIL,queryResult);
        }else{
            return new QueryResponseResult<>(CommonCode.SUCCESS,queryResult);
        }
    }

    @PostMapping("queryonce")
    @ResponseBody
    public QueryResponseResult<Video> queryOnce(@RequestParam("id") Long id){
        QueryResult<Video> queryResult=new QueryResult<>();
        List<Video> videoList=videoService.queryOnce(id);
        this.setStringId(videoList);
        queryResult.setList(videoList);
        queryResult.setTotal(videoList.size());
        if(videoList.isEmpty()){
            return new QueryResponseResult<>(CommonCode.FAIL,queryResult);
        }else{
            return new QueryResponseResult<>(CommonCode.SUCCESS,queryResult);
        }
    }

    @PostMapping("add")
    @ResponseBody
    public ResponseResult addVideo(@RequestBody Video video){
        if(videoService.addVideo(video)==1){
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);
    }

    @PostMapping("del")
    @ResponseBody
    public ResponseResult delVideo(@RequestBody Video video){
        if(videoService.delVideo(video.getId())==1){
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);
    }

    @PostMapping("queryrecordednew")
    @ResponseBody
    public QueryResponseResult<Video> queryRecordedNew(@RequestParam("course_id") Long course_id){
        QueryResult<Video> queryResult=new QueryResult<>();
        List<Video> videoList=videoService.queryRecordedNew(course_id);
        this.setStringId(videoList);
        queryResult.setList(videoList);
        queryResult.setTotal(videoList.size());
        if(videoList.isEmpty()){
            return new QueryResponseResult<>(CommonCode.FAIL,queryResult);
        }else{
            return new QueryResponseResult<>(CommonCode.SUCCESS,queryResult);
        }

    }


    //格式化输出数据
    private List<Video>  setStringId(List<Video> videoList){
        for(Video video:videoList){
            video.setHtmlId(video.getId().toString());
        }
        return videoList;
    }
}
